<?php
// +----------------------------------------------------------------------
// | YFCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: rainfer <81818832@qq.com>
// +----------------------------------------------------------------------
namespace app\home\controller;

use think\Cache;
use think\Db;
use think\captcha\Captcha;

class Product extends Base
{
	public function index()
    {
        $cId = !empty(input("id"))?input("id"):0;
        if (empty($cId)) {
            $map = 0;
        }else {
            $map['cid'] = $cId;
        }
        $this->getCate();
        //获取分类名
        $cate_name = Db::name("category")->where('id='.$cId)->field("name")->find();
        $c_name = !empty($cate_name)?$cate_name['name']:"全部产品";
        $product = Db::name("product")->where($map)->order("sort asc")->paginate(20,false,['query'=>get_query()]);
        $show = $product->render();
        $this->assign("num",count($product));
        $this->assign('page',$show);
        $this->assign('c_name',$c_name);
        $this->assign("product",$product);
		return $this->view->fetch(':product');
	}

	
	/**
     * 联系方式
     */
    public function proDetail()
    {
        $id = !empty(input("id"))?input("id"):0;
        if ( empty($id)) {
            $this->error("参数有误！");
        }
        $detail = Db::name("product")->where('id='.$id)->find();
        $this->getCate();
        $this->assign("detail",$detail);
        return $this->view->fetch(':productDetail');
	}

    public function getCate()
    {
        //获取分类
        $cate = Db::name("category")->order("sort asc")->select();
        $this->assign("cate",$cate);
	}
	
	/**
     * 产品搜索
     */
    public function searchPro()
    {
        $k = input("keyword");
        $this->getCate();
        //获取分类名
        $c_name = "全部产品";
        $product = Db::name("product")->where('name','like',"%$k%")->order("sort asc")->paginate(20,false,['query'=>get_query()]);
        $show = $product->render();
        $this->assign("num",count($product));
        $this->assign('page',$show);
        $this->assign('c_name',$c_name);
        $this->assign("product",$product);
        return $this->view->fetch(':product');
	}

}